package hypeerweb.visitor;

import hypeerweb.node.Node;

/**
 * A class where you make the operation function that is called
 * on every node in the HyPeerWeb _assuming that the BroadcastHelper
 * goes to the 0th node before starting the broadcast._
 * @author Levi Schuck
 *
 */
public abstract class BroadcastVisitor implements Visitor {

	protected static final String STARTED_KEY = "started key";

	public BroadcastVisitor() {
		
	}

	protected abstract void operation(Node node, Parameters parameters);

	public static Parameters createInitialParameters() {
		return new Parameters();
	}

	public void visit(Node node, Parameters parameters) {
		parameters.set(STARTED_KEY, true);
		operation(node, parameters);
		char[] webId = node.getWebId().toString().toCharArray();
		int index = 0;
		for (char digit : webId) {
			if (digit == '1') break;
			webId[index] = '1';
			int id = Integer.parseInt(new String(webId), 2);
			node.getNodeByWebId(id).accept(this, parameters);
			webId[index] = '0';
			index++;
		}
	}
}
